//----------------------------------Include-------------------------------------
#include "mcu.h"
//------------------------------------------------------------------------------

//6 个通用定时器，每个通用定时器 4 个通道，每个通道一个 32 位计数器
//4 个定时器位于系统电源域称为通用定时器 GPTMR0∼3
//1 个定时器位于系统电源域的 CONN 子系统，称为网络定时器 NTMR0
//1 个定时器位于电源管理域，称为电源管理域定时器 PTMR

//-----------------------------------Macro--------------------------------------
#define _TIMER_UNIT              HPM_GPTMR0       //gp timer 0/1/2/3/4
#define _TIMER_CHN               0                //channel  0/1/2/3/4
#define _TIMER_CLK               clock_gptmr0
#define _INT_IRQn                IRQn_GPTMR0
//------------------------------------------------------------------------------

//--------------------------------Static Var------------------------------------
static uint32_t    src_clk;
static uint32_t    clk_div;
static uint32_t    req_freqHz;
static uint32_t    period_cnt;
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
//------------------------------------------------------------------------------

//------------------------------------ISR---------------------------------------
#if 1
SDK_DECLARE_EXT_ISR_M(_INT_IRQn, _isr_gptimer0);
void _isr_gptimer0(void)
{
  if( gptmr_check_status(_TIMER_UNIT, GPTMR_CH_RLD_STAT_MASK(_TIMER_CHN)) ){
    gptmr_clear_status(_TIMER_UNIT, GPTMR_CH_RLD_STAT_MASK(_TIMER_CHN));

#if 0
    //imcu_digit_input();

    static unsigned int cnt;
    if(++cnt >= 1000){
      cnt = 0;
      mcu_led_toggle();
    }
#endif
  }
}
#endif
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
void mcu_init_tim_g0(void)
{
//periph clock
  clock_add_to_group(_TIMER_CLK, 0);

//timer base
  src_clk    = clock_get_frequency(_TIMER_CLK);
  clk_div    = 1;
  req_freqHz = 1000;
  period_cnt = (src_clk/clk_div)/req_freqHz;
  
  gptmr_channel_config_t    config;
  gptmr_channel_get_default_config(_TIMER_UNIT, &config);
  
  config.reload = period_cnt;
  gptmr_channel_config(_TIMER_UNIT, _TIMER_CHN, &config, false);

#if 1
//interrupt
  gptmr_enable_irq(_TIMER_UNIT, GPTMR_CH_RLD_IRQ_MASK(_TIMER_CHN));
  intc_m_enable_irq_with_priority(_INT_IRQn, 15);
#endif

//enable
  gptmr_start_counter(_TIMER_UNIT, _TIMER_CHN);
}
//------------------------------------EOF---------------------------------------





